<?php

namespace App\Listeners;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;

/**
 * Sql语句打印监听器
 *
 * @package App\Listeners
 * @author: WangBin
 * @date: 2021/7/26
 * @mail: <849850352@qq.com>
 */
class DBSqlListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  QueryExecuted  $event
     * @return void
     */
    public function handle(QueryExecuted $event)
    {
        return;
        if(!app()->environment(['testing', 'local'])) {
            return;
        }
        $sql = $event->sql;
        $bindings = $event->bindings;
        $time = $event->time;
        foreach ($bindings as &$binding)
        {
            if(is_string($binding)) {
                $binding = "'$binding'";
            }
        }
        $sql = str_replace('?', '%s', $sql);
        $sql = sprintf($sql, ...$bindings);
        Log::info('sql 语句:', ['sql' => $sql, 'time' => $time]);
    }
}
